import * as bm from "/js/baseModule.js";

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        this.typeId = 2;
        let selected_Pos = globalData.selPos;
        let that = this;
        switch (globalData.currType) {
            case "1": this._currType = 'index'; break;
            case "2": this._currType = 'repair'; break;
            case "3": this._currType = 'rental'; break;
            case "4": this._currType = 'purchase'; break;
        }

        /*vue*/
        let CreateDemand_vue = this.find(".CreateDemand_vue")[0];
        new Vue({
            el: CreateDemand_vue,
            data: {
                currentType: that._currType,
                selected_Pos: selected_Pos,
            },
        });
        that.purchaseText = that.find('.isSelected').parent().text();
        this.errorTip = this.find(".errorTip");
        $('.input-ss').on("input", function () {
            that.errorTip.hide();
        });
        //选择类型
        $(".choice-type").each(function (index, element) {
            let that_son = $(this);
            that_son.on("click", function () {
                $(".select").removeClass("isSelected");
                that_son.find('.select').addClass("isSelected");
                that.typeId = that_son.find('.select').attr('type');
                //采购的其他
                if (that.typeId == '0' && that._currType == 'purchase') {
                    that.find(".otherType").show();
                } else {
                    that.find(".otherType").hide();
                }
                that.purchaseText = that_son.text();
                that.errorTip.hide();
            });
        });
        //维修项目第二屏选择
        $(".choice-fault").each(function (index, element) {
            let that_son = $(this);
            that_son.on("click", function () {
                $(".select").removeClass("faultSelected");
                that_son.find('.select').addClass("faultSelected");
            });
        });
        //屏幕类型下一步
        $(".inquiry-type-next").on("click", function () {
            if (that.purchaseText == '其他' && that._currType == 'purchase') {
                if (!that.checkPurchaseOther(that.find(".otherType").val().trim(), 'other')) {
                    return false;
                }
            }
            $("#TypeSelect").addClass("hide");
            $("#ColorSelect").removeClass("hide");
            $("#LastSelect").addClass("hide");
            if (that._currType == 'index') {
                that.updateInit(that.typeId);
            }

        });
        //第二屏下一步
        $(".inquiry-color-next").on("click", function () {
            //租赁和制作要验证面积，
            //维修是单选项
            //采购不用验证
            if (that._currType == "index" || that._currType == "rental") {
                if (that.checkSpan('span', "间距") && that.checkSize(that.find('.size').val(), 'size', "面积")) {
                    $("#TypeSelect").addClass("hide");
                    $("#ColorSelect").addClass("hide");
                    $("#LastSelect").removeClass("hide");
                }
            } else {
                $("#TypeSelect").addClass("hide");
                $("#ColorSelect").addClass("hide");
                $("#LastSelect").removeClass("hide");
            }
        });
        //第二屏上一步
        $(".inquiry-color-prv").on("click", function () {
            $("#TypeSelect").removeClass("hide");
            $("#ColorSelect").addClass("hide");
            $("#LastSelect").addClass("hide");
        });
        //last上一步
        $(".inquiry-last-prv").on("click", function () {
            $("#TypeSelect").addClass("hide");
            $("#ColorSelect").removeClass("hide");
            $("#LastSelect").addClass("hide");
        });

        $(document).on('data_update',function () {
            //如果是游客/客户
            if (globalData.user.user_type != 100) {
                let type = 1;
                if(globalData.user.user_type == 1){
                    type = 2;
                }
                funs.openModal('modules/modals/CommonModal.js',function (mod) {
                    mod.setType(type);
                });
            }
        })

    }
    /**间距校验 */
    checkSpan(dom, tip) {
        let reg = /^[\u4e00-\u9fa5A-Za-z0-9-.]*$/;
        if (this.find(".custom-options-button").hasClass("hide")) {
            this.span = this.find('.span').val()
        } else {
            this.span = this.find('.spanChoosed').text();
        }
        if (this.span == "") {
            $('.' + dom + 'Tip').show();
            $('.' + dom + 'Tip').find('.error-span').text(tip + '不能为空！');
            return false;
        } else if (!reg.test(this.span)) {
            $('.' + dom + 'Tip').show();
            $('.' + dom + 'Tip').find('.error-span').text('请输入正确的' + tip);
            return false;
        } else {
            return true;
        }
    }
    /**面积校验 */
    checkSize(info, dom, tip) {
        let reg = /^\d+(\.\d+)?$/;
        if (info == "") {
            $('.' + dom + 'Tip').show();
            $('.' + dom + 'Tip').find('.error-size').text(tip + '不能为空！');
            return false;
        } else if (!reg.test(info)) {
            $('.' + dom + 'Tip').show();
            $('.' + dom + 'Tip').find('.error-size').text('请输入正确的' + tip);
            return false;
        } else {
            return true;
        }
    }
    /**昵称校验 */
    checkNick(info, dom, tip) {
        let reg = /^[\u4e00-\u9faf]+$/;
        if (info == "") {
            $('.' + dom + 'Tip').show();
            $('.' + dom + 'Tip').find('.error-nick').text(tip + '不能为空！');
            return false;
        } else if (!reg.test(info)) {
            $('.' + dom + 'Tip').show();
            $('.' + dom + 'Tip').find('.error-nick').text('请输入正确的' + tip);
            return false;
        } else {
            return true;
        }
    }
    /**采购其他校验 */
    checkPurchaseOther(info, dom) {
        if (info == "") {
            $('.' + dom + 'Tip').show();
            return false;
        } else {
            return true;
        }
    }

    /**本来想写一个来着，但是取值不好取，就写了三个 */
    _evt_select1(target, hit) {
        this.locSelect(target);
        return false;
    }
    _evt_select2 (target, hit) {
        this.corSelect(target);
        return false;
    }
    _evt_select3 (target, hit) {
        this.spanSelect(target);
        return false;
    }

    /**实现选择屏幕联动改变颜色、位置的初始值 */
    updateInit(scrType) {
        let list = this.find('.choose');

        for (let i = 0; i < list.length; i++) {
            if (scrType == 0) {
                if ($(list[i]).attr('loc') == "1") {
                    this.locSelect(list[i]);
                }
                if ($(list[i]).attr('color') == "3") {
                    this.corSelect(list[i]);
                }
                this.difSpan('P12', 'P10', 'P8');
                if ($(list[i]).text() == "P10") {
                    this.spanSelect(list[i]);
                }
            } else if (scrType == 1) {
                if ($(list[i]).attr('loc') == "3") {
                    this.locSelect(list[i]);
                }
                if ($(list[i]).attr('color') == "1") {
                    this.corSelect(list[i]);
                }
                this.difSpan('P12', 'P10', 'P8');
                if ($(list[i]).text() == "P10") {
                    this.spanSelect(list[i]);
                }
            } else if (scrType == 2) {
                if ($(list[i]).attr('loc') == "1") {
                    this.locSelect(list[i]);
                }
                if ($(list[i]).attr('color') == "3") {
                    this.corSelect(list[i]);
                }
                this.difSpan('P6', 'P8', 'P10');
                if ($(list[i]).text() == "P10") {
                    this.spanSelect(list[i]);
                }
            } else if (scrType == 3) {
                if ($(list[i]).attr('loc') == "1") {
                    this.locSelect(list[i]);
                }
                if ($(list[i]).attr('color') == "1") {
                    this.corSelect(list[i]);
                }
                this.difSpan('P6', 'P8', 'P10');
                if ($(list[i]).text() == "P10") {
                    this.spanSelect(list[i]);
                }
            } else if (scrType == 4) {
                if ($(list[i]).attr('loc') == "2") {
                    this.locSelect(list[i]);
                }
                if ($(list[i]).attr('color') == "3") {
                    this.corSelect(list[i]);
                }
                this.difSpan('P3', 'P4', 'P5');
                if ($(list[i]).text() == "P4") {
                    this.spanSelect(list[i]);
                }
            } else {
                if ($(list[i]).attr('loc') == "2") {
                    this.locSelect(list[i]);
                }
                if ($(list[i]).attr('color') == "3") {
                    this.corSelect(list[i]);
                }
                this.difSpan('P1.25', 'P2.0', 'P2.5');
                if ($(list[i]).text() == "P2.0") {
                    this.spanSelect(list[i]);
                }
            }
        }
    }
    // 
    /**选择不同类型屏幕对应默认不同的间距*/
    difSpan(p1, p2, p3) {
        $($('.span3 .choose')[0]).text(p1);
        $($('.span3 .choose')[1]).text(p2);
        $($('.span3 .choose')[2]).text(p3);
    }
    /**选择一种类型之后清除其他选项的底色，三个方法 */
    locSelect(dom) {
        $(dom).parent().parent().find('.choose').removeClass('locChoosed');
        $(dom).addClass('locChoosed')
    }
    corSelect(dom) {
        $(dom).parent().parent().find('.choose').removeClass('corChoosed');
        $(dom).addClass('corChoosed')
    }
    spanSelect(dom) {
        $(dom).parent().parent().find('.choose').removeClass('spanChoosed');
        $(dom).addClass('spanChoosed')
    }
    /**inquirySubmit */
    _evt_inquirySubmit() {
        let that = this;
        let mobile = globalData.user.mobile;
        let nick = globalData.user.nick;
        let type = that.find('.isSelected').attr('type');
        let size = that.find('.size').val();
        let location = that.find('.locChoosed').attr('loc');
        let color = that.find('.corChoosed').attr('color');
        let city = that.find('.citySelect').text();
        let prov = globalData.selPos.prov;
        let addr = that.find(".addr").val().trim();
        let span = that.span;
        let fault = that.find('.faultSelected').attr('fault');
        let cate = parseInt(globalData.currType);

        let prePostData = {
            mobile: mobile,
            cate: cate,
            prov: prov,
            city: city,
            sem: JSON.stringify({ "source": "微信" })
        };

        /*预发布需求，产品需要提前知道手机号，*/
        api.prePost(prePostData, {
            succ: function (data) {
                let id = data.id;
                let ActivateData = {
                    id: id,
                    nickname: nick,
                    type: type,
                    surr: location,
                    size: size,
                    color: color,
                    span: span,
                    prov: prov,
                    city: city,
                    addr: addr,
                    malf: fault,
                };

                if (cate == 4) {
                    let PurchaseActivateData = {
                        id: id,
                        prov: prov,
                        city: city,
                        addr: addr,
                        product_type_name: that.purchaseText,
                        batch_num: that.find(".batch").val().trim(),
                        product_model: that.find(".model").val().trim(),
                        product_amount: that.find(".number").val().trim() || 0,
                    };
                    //采购单独一个接口
                    api.prePostPurchaseDemand(PurchaseActivateData, {
                        succ:function (data) {//发布成功，，直接打开聊天室（“已登录，无在建项目”，询价）
                            funs.tip("提交成功", "稍后客服将来电跟进您的需求", "succ");
                            setTimeout(function () {
                                window.location.href = "/index.html";
                            }, 3000);
                        },
                        fail: function (error) {
                            if (error.status == "41004002") {
                                //项目不存在
                                funs.tip(error.msg, "", "fail");
                                setTimeout(function () {
                                    window.location.href = "/index.html";
                                }, 2000);
                            } else {
                                funs.tip(error.msg, "", "fail");
                                setTimeout(function () {
                                    window.location.href = "/index.html";
                                }, 2000);
                            }
                        }
                    });
                } else {
                    api.activateDemand(ActivateData, {
                        succ: function (data) {
                            //发布成功
                            funs.tip("提交成功", "稍后客服将来电跟进您的需求", "succ");
                            setTimeout(function () {
                                window.location.href = "/index.html";
                            }, 3000);
                        },
                        fail: function (error) {
                            if (error.status == "41004002") {
                                //项目不存在
                                funs.tip(error.msg, "", "fail");
                                setTimeout(function () {
                                    window.location.href = "/index.html";
                                }, 2000);
                            } else {
                                funs.tip(error.msg, "", "fail");
                                setTimeout(function () {
                                    window.location.href = "/index.html";
                                }, 2000);
                            }
                        }
                    });
                }
            },
            fail:function (error) {
                funs.tip('提交失败',error.msg,'fail');
                setTimeout(function () {
                    window.location.href = "/index.html";
                }, 3000);
            }
        });




        return false;
    }
    /**城市选择 */
    _evt_citySelect() {
        location.hash = 'common/CitySelector';
    }
    /**报装间距自定义 */
    _evt_customOptionsInput() {
        this.find(".custom-options-button").addClass("hide");
        this.find(".custom-options-input").removeClass("hide");
    }
    /**报装间距返回 */
    _evt_customOptionsChoose() {
        this.find(".custom-options-input").addClass("hide");
        this.find(".custom-options-button").removeClass("hide");
    }
}